class Scene extends GuaScene {
    constructor(game) {
        super(game)
        self.game = game

        this.game.scene = this
        this.setUp()
        this.setInputs()

    }
    setUp() {
        // this.particles = GuaParticleSystem.new(this.game)
        // this.addElement(this.particles)


        var bg = GuaImage.new(this.game, 'bg')
        this.bg = bg
        this.addElement(bg)

        var pipe = new Pipe(this.game) 
        this.pipe = pipe
        this.addElement(pipe)

        var ground = new Ground(this.game)
        this.ground = ground
        this.addElement(ground)

        var score = new Nes(this.game)
        this.score = score
        this.addElement(score)

        var bird = new Bird(this.game)
        this.bird = bird
        bird.setPosition(110, 180)
        this.addElement(bird)

    }

    setInputs() {
        var self = this
        // self.game.registerAction('d', function(keyStatus) {
        this.jumpCount = 10
        this.startCount = 50
        //     self.bird.run(+9, keyStatus)
        // })
        // self.game.registerAction('a', function(keyStatus) {
        //     self.bird.run(-7, keyStatus)
        // })
        self.game.registerAction('j', function(keyStatus) {
            self.bird.jump(config.birdJumpHeight.value, keyStatus)
        })
    }

    draw() {
        super.draw()
            // this.game.context.strokeText('开始游戏', 20, 250)
    }

    update() {
        super.update()
        this.jumpCount -= 1
        this.startCount -= 1
        if (this.startCount > 0 & this.jumpCount < 0) {
            log("this",this.startCount)
            this.bird.jump(config.birdJumpHeight.value, "keydown")
            this.jumpCount = 12
        }
        let pipes = this.pipe.pipes 
        for (let p of pipes) {
            // 通过 pipe 得分
            if (0 < p.x && p.x < 300 && p.y > 0) {
                if (p.x > 200) {
                    p.passed = false
                    if (this.bird.texture.y < (p.y + this.bird.texture.h)) {
                        if (this.jumpCount < 0) {
                            this.bird.jump(config.birdJumpHeight.value, "keydown")
                            this.jumpCount = 15
                        }
                    }
                }
                if (p.passed == false && p.x < (this.bird.x - p.w)) {
                    p.passed = true
                    this.score.number += 100
                }
            }
                if (rectIntersect(this.bird.texture, p)) {
                    this.bird.alive = false
                }
        }
        if (this.bird.y >= 300) {
            this.bird.alive = false
        }

        if (this.bird.alive == false) {
            let end = new SceneEnd(this.game)
            end.addElements(this.bg, this.pipe, this.bird, this.ground)
            this.game.addScene('end', end)
            this.game.runWithScene('end')
        }
    }

}